<script type="text/javascript">
	$(function(){
		$('#closeSameWord').click(function(){
			$('#sameWordFloat').hidePop(300);
			$('#sameWordSearchWord').val('');
		});
		
		$('#sameWordSearchBtn').bind('click', function(){
			searchSameWord();
		});
		
		$('span[name="addBtn"]').live('click', function(){
			var o = $(this);
			var id = o.parent().find('input.sameWordHiddenId').val();
			$('#sameWordHiddenId').val(id);
			$('#sameWordNewKey').val('');
			var x = o.position().left + o.width() + 20;
			var y = o.position().top + o.height() + 3;
			$('#sameWordInputDiv').css({'left':x+'px','top':y+'px'}).fadeIn(0);
			$('#sameWordNewKey').focus();
		});
		
		
		$('span[name="_addBtn"]').live('click', function(){
			var o = $(this);
			var id = o.parents('.bgTr').find('.checkBoxes').val();
			
			$('#sameWordHiddenId').val(id);
			$('#_sameWordNewKey').val('');
			var x = o.position().left - o.width() - 180;
			var y = o.position().top - o.height() - 12;
			$('#_sameWordInputDiv').css({'left':x+'px','top':y+'px'}).fadeIn(0);
			$('#_sameWordNewKey').focus();
		});
		
		$('#sameWordFloat').bind('click', function(e){
			var tar = $(e.target);
			var id = tar.attr('id');
			if(id == 'sameWordInputDiv' || id == 'sameWordNewKey' || id == 'confirmAddNewKey'){
				return;
			}
			$('#sameWordInputDiv').fadeOut(0);
		});
		
		$(window).bind('click', function(e){
			var tar = $(e.target);
			
			var id = tar.attr('id');
			if(id == '_sameWordInputDiv' || id == '_sameWordNewKey' || id == '_confirmAddNewKey' || tar.attr('name') == '_addBtn'){
				return;
			}
			$('#_sameWordInputDiv').fadeOut(0);
			return;
		});
		
		initCateSelect('same_word', 'sameWordSelect');
		
		$('#titleDiv1').click(function(){
			$('#sameWordSearchWord').focus();
		});
	});
	function searchSameWord(){
		var key = $('#sameWordSearchWord').val();
		if(key.length < 1){
			return;
		}
		key = encodeURI(key);
		ajax('looksame?keyword=' + key, function(rs){
			var h = '';
			if(rs && rs.length > 4){
				var list = eval(rs);
				for(var i = 0, len = list.length; i < len; i ++){
					var o = list[i];
					var id = o.id;
					var ks = o.keyWord;
					h += '<div class="oneSameWord" >';
					for(var j = 0, jl = ks.length; j < jl; j ++){
						var k = ks[j];
						h += '<span class="sameWordkeyspan">' + k + '</span><span style="clear:both"></span>';
					}
					h += '<input type="hidden" value="'+id+'" class="sameWordHiddenId" />';
					h += '<span name="addBtn" style="margin-bottom:-5px;margin-left:6px;float:left;" title="点击添加" class="addBtn">+</span>';
					h += '</div>';
				}
			}else{
				h = '<div style="padding:20px;font-size:13px;color:red;">没有相关同义词信息 ( ⊙o⊙ )千真万确</div>';
			}
			$('#sameWordList').html(h);
		});
	}
	function confirmAddNewKey(n){
		var key = $('#sameWordNewKey').val();
		var id = $('#sameWordHiddenId').val();
		if(key.length < 1 || id.length < 1){
			alert('请输入关键字或选择同义词词条');
			return;
		}
		key = encodeURI(key);
		ajax('insertsame?id=' + id + '&keyword=' + key, function(rs){
			$('#sameWordInputDiv').fadeOut(0);
			searchSameWord();
		});
	}
	function _confirmAddNewKey(n){
		var key = $('#_sameWordNewKey').val();
		var id = $('#sameWordHiddenId').val();
		if(key.length < 1 || id.length < 1){
			alert('请输入关键字或选择同义词词条');
			return;
		}
		key = encodeURI(key);
		ajax('insertsame?id=' + id + '&keyword=' + key, function(rs){
			$('#_sameWordInputDiv').fadeOut(0);
			ListData();
		});
	}
	function addNew(obj){
		var o = $('#addNewSameWordBar');
		if(o.data('v') == null || o.data('v') == 0){
			setAddNewSts(1);
		}else{
			setAddNewSts(0);
		}
	}
	
	function doAddNew(){
		var keys = $('#addNewSameWordInput').val();
		var cid = $('#sameWordSelect').val();
		if(keys.length < 1){
			alert('不能为空');
			return;
		}
		keys = encodeURI(keys);
		ajax('newsame?keyword=' + keys + '&cateid=' + cid, function(rs){
			searchSameWord();
			$('#addNewSameWordInput').val('');
			$('#sameWordDoAddNew,#sameWordSelect,#addNewSameWordInput').fadeIn(0);
			setAddNewSts(0);
			reloadPage();
		});
	}
	
	function setAddNewSts(n){
		var o = $('#addNewSameWordBar');
		var tarInput = $('#addNewSameWordInput');
		if(n && n > 0){
			o.data('v', 1);
			tarInput.fadeOut(0).css('width','0px').fadeIn(0).val('');
			tarInput.animate({width:225}, 500, function(){
				tarInput.focus();
			});
			$('#sameWordDoAddNew,#sameWordSelect').fadeIn(500);
			$('#addNewSameWordBar').val('新增同义词条 ◁');
		}else{
			o.data('v', 0);
			tarInput.animate({width:0}, 0, function(){tarInput.fadeOut(0);});
			$('#sameWordDoAddNew,#sameWordSelect').fadeOut(0);
			$('#addNewSameWordBar').val('新增同义词条 ▷');
		}
	}
</script>
<div id="sameWordFloat" class="wordFloat">
	<div id="titleDiv" class="titleDiv">
	  	<div id="closeSameWord" class="closeDiv" >×</div>
	  	<div style="float:left;margin-top:-5px;">同义词编辑</div>
	</div>
	<div style="padding-top:15px;padding-left:15px;padding-right:15px;padding-bottom:4px;">
		<input type="text" class="sameImput" value="新增同义词条" readOnly="readOnly" id="sameKeyword" />
		<div style="margin-top:20px;">
			<div id="titleDiv1" style="height:30px;width:380px;font-size:15px;color:#dddddd;margin-bottom:-37px;text-align:right;padding-top:7px;">
				输入关键字，查看已存在的同义词词条
			</div>
			<span class="font2">关键字：</span><input class="sameImput" style="width:320px;background:transparent;" value="" type="text" id="sameWordSearchWord" />
			&nbsp;&nbsp;&nbsp;&nbsp;
			<input class="btnStyle1" type="button" value="查看" id="sameWordSearchBtn" />
			<div id="sameWordAutoflow" >
				<div id="sameWordList">
				
				</div>
			</div>
		</div>
	</div>
	<div style="clear:both;"></div>
	<div style="padding-left:15px;padding-top:2px;">
		<div style="height:37px;margin-bottom:-37px;margin-left:120px;padding-top:1px;font-size:13px;color:red;">
			点击按钮，添加新词条；至少两个词，逗号或分号隔
		</div>
		<table>
			<tr>
				<td width="120">
					<input type="button" value="新增同义词条 ▷" class="wordBarInput" id="addNewSameWordBar" onclick="addNew(this);" />
				</td>
				<td width="240">
					<input type="text" title="多个词之间逗号或分号隔开" value="" id="addNewSameWordInput" style="height:23px;border-radius:4px;border:1px solid green;padding-left:5px;width:0px;display:none;" />
				</td>
				<td >
					<select id="sameWordSelect" style="border-radius:4px;height:23px;display:none;"></select>
				</td>
				<td >
					<input type="button" style="display:none;" value="ok" class="btnStyle1" id="sameWordDoAddNew"  onclick="doAddNew();" />
				</td>
			</tr>
		</table>
	</div>
	<div id="sameWordInputDiv" style="width:200px;">
		<input type="hidden" value="" id="sameWordHiddenId" />
		<input type="text" title="多个词之间逗号或分号隔开" id="sameWordNewKey" style="height:23px;width:130px;" />
		<input type="button" id="confirmAddNewKey" value="确定" onclick="confirmAddNewKey();" />&nbsp;&nbsp;
	</div>
</div>

<div id="_sameWordInputDiv" >
	<input type="text" title="多个词之间逗号或分号隔开" id="_sameWordNewKey" style="height:23px;width:100px;" />
	<input type="button" id="_confirmAddNewKey" value="确定" onclick="_confirmAddNewKey();" />&nbsp;<span style="color:red;font-weight:bold;">-></span>
</div>